#pragma once
#ifndef __DATAMAP_HPP__
#define __DATAMAP_HPP__

#include <unordered_map>
#include <mutex>
#include <condition_variable>
#include <iostream>

template<typename KeyType, typename ValueType>
class DataMap{
public:
    DataMap() = default;

    void put(KeyType const& key, ValueType val){
        {
        std::unique_lock<std::mutex> lock(mtx);
        data_map.emplace(key, val);
        }
        cond.notify_all();
    }

    std::pair<ValueType,bool> get(KeyType const& key, int timeout_sec){
        std::unique_lock<std::mutex> lock(mtx);
        cond.wait_for(lock, std::chrono::seconds(timeout_sec), [this, &key](){ return this->data_map.count(key) != 0;});
        std::pair<ValueType,bool> data;
        if (this->data_map.count(key)){
            data.first = data_map.at(key);
            data.second = true;
            data_map.erase(key);
        }
        return data;
    }

private:
    std::unordered_map<KeyType, ValueType> data_map;
    std::mutex mtx;
    std::condition_variable cond;
};

#endif // __DATAMAP_HPP__